package com.exp.base.util;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.util.Assert;


public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	private static FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
	private static FastDateFormat fastSqlDateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
	private static FastDateFormat fastSqlTimeFormat = FastDateFormat.getInstance("HH:mm:ss");
	/**
	 * 格式化为字符串
	 * 默认为 yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String format(Date date){
		Assert.notNull(date, "date 不能为空");
		return fastDateFormat.format(date);
	}
	
	/**
	 * 格式化为字符串
	 * @param date
	 * @return
	 */
	public static String format(Date date,String pattern){
		Assert.notNull(date, "date 不能为空");
		Assert.notNull(pattern, "pattern 不能为空");
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 读取日期，默认格式为
	 * yyyy-MM-dd HH:mm:ss
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String source) throws ParseException{
		Assert.notNull(source, "source 不能为空");
		return fastDateFormat.parse(source);
	}
	
	/**
	 * 解析为 sql time
	 * 默认格式为 HH:mm:ss
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static java.sql.Time parseSqlTime(String source) throws ParseException{
		Assert.notNull(source, "source 不能为空");
		return new java.sql.Time(fastSqlTimeFormat.parse(source).getTime());
	}
	
	/**
	 * 解析为 sql date
	 * 默认格式为 yyyy-MM-dd
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static java.sql.Date parseSqlDate(String source) throws ParseException{
		Assert.notNull(source, "source 不能为空");
		return new java.sql.Date(fastSqlDateFormat.parse(source).getTime());
	}
	
	/**
	 * 转化为sql date
	 * @param date
	 * @return
	 */
	public static java.sql.Date toSqlDate(Date date){
		Assert.notNull(date, "date 不能为空");
		return new java.sql.Date(date.getYear(),date.getMonth(),date.getDate());
	}
	
	/**
	 * 转化为sql time
	 * @param date
	 * @return
	 */
	public static java.sql.Time toSqlTime(Date date){
		Assert.notNull(date, "date 不能为空");
		Time time =   new java.sql.Time(date.getHours(),date.getMinutes(),date.getSeconds());
		return time;
	}
	

}
